package io.github.jesse205.dragdrop.pro.extensions

import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import io.github.jesse205.dragdrop.pro.app.BaseActivity

val Fragment.thisActivity get() = requireActivity() as BaseActivity

fun Fragment.setupToolbar(toolbar: Toolbar, isBackButtonEnabled: Boolean = false) {
    toolbar.apply {
        title = findNavController().currentDestination?.label
        if (isBackButtonEnabled) {
            setNavigationIcon(androidx.appcompat.R.drawable.abc_ic_ab_back_material)
            setNavigationContentDescription(androidx.appcompat.R.string.abc_action_bar_up_description)
            setNavigationOnClickListener { if (!findNavController().popBackStack()) thisActivity.finish() }
        }
    }
}
